// pages/payment/cancelPay/index.js
import Common from '../../../utils/common.js'
let http = new Common()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '00:00',
    checkIndex: [],
    checkList: [{ name: '我的钱包', icon: '../../../utils/img/036.png' }, { name: 'K币支付', icon: '../../../utils/img/037.png' }, ],
    // { name: '微信支付', icon: '../../../utils/img/038.png', }

    //modle框
    index99: false,
    MoneyDetailShow: false,

    //得到的值
    info: '',

    payMoney: '',
    timesShow:true
  },
  dates: function () {
    let that = this
    // let newDate = new Date()

    let timer
    // let newDate = Commons.deepCopy(oldDate)
    // newDate = newDate.setMinutes(newDate.getMinutes() + 15)
    let newDate = this.data.info.payTimeOut * 1000

    timer = setInterval(function () {
      let oldDate = new Date()

      let t = Math.floor((newDate - oldDate) / 1000)

      if (t + 1 > 0) {
        let minute = Math.floor(t % 86400 % 3600 / 60) < 10 ? `0${Math.floor(t % 86400 % 3600 / 60)}` : Math.floor(t % 86400 % 3600 / 60)
        let second = t % 60 < 10 ? `0${t % 60}` : t % 60

        let str = minute + ':' + second;
        that.setData({
          date: str
        })
        // if (parseFloat(minute) == '0' && parseFloat(second)="")
        // console.log(parseFloat(minute))
        // console.log(parseFloat(second))
      } else {
        clearInterval(timer)
       
      }
    }, 1000)
  },
  getData: function () {
    let tel = wx.getStorageSync('tel')
    this.setData({
      tel: tel
    })
    let that = this
    http.http({
      url: "mp2/store/gotopay",
      data: {
        viptel: tel,
        orderNo: that.data.roomorderno || 'B5bd02628e09f910'
      },
      success(res) {
        // console.log(res.data.data.info)
        let info = res.data.data.info
        let checkList = that.data.checkList
        checkList[0].count = info.bal
        checkList[1].count = info.kq

        that.setData({
          info: info,
          checkList: checkList
        })

        that.dates()
        // console.log(checkList)
      }
    })
  },
  index99: function () {
    this.setData({
      index99: false,
      MoneyDetailShow: false
    })
  },
  MoneyDetails: function () {
    this.setData({
      index99: true,
      MoneyDetailShow: true
    })
  },
  checkboxChange: function (e) {

    let value = e.detail.value
    let checkList = this.data.checkList
    let totalmoney = this.data.info.totalmoney

    let wxShow = false
    
    checkList.forEach((val, index) => {
      if (value.indexOf(val.name) > -1) {
        val.checked = true
      }else{
        val.checked = false
      }
    })

    this.setData({
      checkList: checkList,
    })

    // console.log(count)
    // this.setData({
    //   checkIndex: e.currentTarget.dataset.index
    // })

  },
  OK: function () {

    let that = this
    let checkList = this.data.checkList
    let totalmoney = this.data.info.totalmoney
    let count = 0
    let balPrice = 0
    let kqPrice = 0

    checkList.forEach((val,index)=>{
      if (val.checked){
        if(index == 0){
          balPrice = val.count
        }
        if (index == 1){
          kqPrice = val.count
        }
      }
    })

    // count = parseFloat(totalmoney) - balPrice - kqPrice
    
    // if (count>0){
    //   olPrice = count
    // }

    http.http({
      url: "mp2/store/getpayurl",
      data: {
        viptel: that.data.tel,
        orderNo: that.data.roomorderno || 'B5bd02628e09f910',
        // olPrice: olPrice,
        balPrice: balPrice,
        kqPrice: kqPrice
      },
      success(res) {
        let payURL = res.data.data.payURL
        if (payURL){
          wx.requestPayment({
            timeStamp: payURL.timeStamp,
            nonceStr: payURL.nonceStr,
            paySign: payURL.paySign,
            signType: payURL.signType,
            package: payURL.package,
            success(res){
              console.log('支付成功')
              http.goUrl({
                method: 'redirectTo',
                url: `../../order/tabDetails/index?id=${'1'}&&orderNum=${that.data.roomorderno}`
              })
            },
            fail(res){
              console.log('支付失败')
              http.goUrl({
                method:'switchTab',
                url:`../../order/tabs/index?id=${'1'}`
              })
              
            }
          })
        }else{
          http.goUrl({
            method:'redirectTo',
            url: `../../order/tabDetails/index?id=${1}&&orderNum=${that.data.roomorderno}`
          })
        }
        // console.log(res)
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    if (options.id){
      this.setData({
        roomorderno: options.id
      })
    }
    
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})